package com.base.llpark;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.location.AMapLocation;
import com.arvin.imagepicker.bean.ImageItem;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.RoadEntity;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.utils.TimePickUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.commonlib.widget.ParkCheckDialog;
import com.base.commonlib.widget.SingleChooseDialog;
import com.base.commonlib.widget.SingleSearchDialog;
import com.base.llpark.bean.CarType;
import com.base.llpark.bean.LlPark;
import com.base.llpark.databinding.ActivityLlparkBinding;
import com.base.llpark.presenter.LlParkPresenter;
import com.base.llpark.utills.CrpuctPrintUtil;
import com.base.llpark.view.LlParkView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Route(path = Constance.ACTIVITY_URL_FIND)
public class IllParkActivity extends CBaseBarMvpActivity<LlParkPresenter> implements LlParkView {
    @Autowired
    public String number,color,path;
    protected ActivityLlparkBinding mBinding;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_llpark;
    }

    protected LlParkPresenter mPresenter;
    private SimpleDateFormat mDateFormat = null;
    private LlPark mPark = null;

    private String mCarType = "";
    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityLlparkBinding) mRootBinding;
        mPark = new LlPark();
        mPresenter = new LlParkPresenter(this, this);
        mPresenter.start();
        setBackNavigationTip(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWaringDialog.show();
            }
        });
        setBarTitle("违停登记");
        mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mBinding.tvDate.setText(mDateFormat.format(new Date()));
        mPresenter.startLocationForLl();
        mBinding.btnSave.setVisibility(View.VISIBLE);
        mBinding.btnUp.setVisibility(View.GONE);
        mBinding.btnPrint.setVisibility(View.GONE);


        String number = getIntent().getStringExtra("number");
        if(!TextUtils.isEmpty(number)&&number.length()>=7){
            mPresenter.initCraNo(getIntent().getStringExtra("number"));
        }else {
            mPresenter.getCarType();
        }

    }

    @Override
    protected void start() {




        //省
        mBinding.tvSheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> strs_sheng = Arrays.asList(mPresenter.mShengs);
                new SingleChooseDialog<String>(mContext, "请选择省份", strs_sheng, new SingleChooseDialog.OnItemChooseListener() {

                    @Override
                    public void onItemChoose(Object o) {
                        mBinding.tvSheng.setText(o.toString());
                    }
                }).show();
            }
        });
        //车牌首字母
        mBinding.tvLetter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> strs_letter = Arrays.asList(mPresenter.mLetters);
                new SingleChooseDialog<String>(mContext, "请选择首字母", strs_letter, new SingleChooseDialog.OnItemChooseListener() {

                    @Override
                    public void onItemChoose(Object o) {
                        mBinding.tvLetter.setText(o.toString());
                    }
                }).show();
            }
        });
        mBinding.tvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickUtil.pickDateTime(mContext,mBinding.tvDate);
            }
        });


        mBinding.ivPop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SingleChooseDialog<RoadEntity>(mContext, "请选择路段", mPresenter.mMMineRoads, new SingleChooseDialog.OnItemChooseListener() {

                    @Override
                    public void onItemChoose(Object o) {
                        mBinding.tvRoad.setText(o.toString());
                    }
                }).show();
            }
        });
        mBinding.tvRoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SingleSearchDialog<RoadEntity>(mContext, "请搜索路段", mPresenter.mRoadEntities, new SingleSearchDialog.OnItemChooseListener() {
                    @Override
                    public void onItemChoose(Object o) {
                        mBinding.tvRoad.setText(o.toString());
                    }
                }).show();
            }
        });


        mBinding.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!checkEditText(mBinding.etNum)||!checkEditText(mBinding.etAddress)||!checkTextView(mBinding.tvRoad)||!checkSpinner(mBinding.spType))
                {
                    showMes("请完善内容!");
                    return;
                }
                String num = mBinding.etNum.getText().toString().trim();
                String s = num.toUpperCase();
                mBinding.etNum.setText(s);
                if(!mPresenter.checkPlateNumberFormat(mBinding.tvSheng.getText().toString()+mBinding.tvLetter.getText().toString()+s))
                {
                    showMes("车牌格式不正确");
                    return;
                }
                if (mBinding.picker !=null) {
                    if (mBinding.picker.getPics().size() < 1) {
                        showMes("照片不能少于1张");
                        return;
                    }
                }else {
                    showMes("请选择图片");
                    return;
                }
                //保存
                mPark.setCarNo(mBinding.tvSheng.getText().toString()+mBinding.tvLetter.getText().toString()+s);//车牌号
                CarType carType = (CarType) mBinding.spType.getSelectedItem();
                mCarType = carType.getDictValue();
                mPark.setCarType(carType.getDictId());//车辆类型
                mPark.setCheckDate(mBinding.tvDate.getText().toString().trim());//日期


                String roadId = mPresenter.getRoadId(mBinding.tvRoad.getText().toString().trim());
                if (TextUtils.isEmpty(roadId)){
                    showMes("未查到路段ID");
                    return;
                }
                mPark.setRoad(roadId);//路段ID
                mPark.setRoadName(mBinding.tvRoad.getText().toString().trim());//路段名称

                mPark.setCheckSignAddress(mBinding.etAddress.getText().toString().trim());
                if (mPresenter.mCurrentPartner!=null){
                    mPark.setPersonId2(mPresenter.mCurrentPartner.getId());//搭档ID
                    mPark.setPersonName2(mPresenter.mCurrentPartner.getUserName());//搭档Name
                }else {
                    mPark.setPersonId2("");//搭档ID
                    mPark.setPersonName2("");//搭档Name
                }
                mPark.setState("-1");
                mPark.setUserName(mPresenter.mUser.getUserName());
                List<String> file = new ArrayList<>();
                for (ImageItem item : mBinding.picker.getPics()){
                    if (item.path != null) {
                        file.add(item.path);
                    }
                }
                mPark.setFiles(file);
                if (mBinding.btnPrint.getVisibility() == View.VISIBLE){
                    mPresenter.updataPark(mPark,false);
                }else {
                    mPresenter.save(mPark);
                }
            }
        });


        //打印
        mBinding.btnPrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPark.setCompanyName(mPresenter.mUser.getCompanyName());
                if (RealNetWorkUtil.TYPE.equals("0")){
                    CrpuctPrintUtil.printPark(mContext,mPark,mCarType);
                }else if (RealNetWorkUtil.TYPE.equals("1")){
                    CrpuctPrintUtil.printParkKS(mContext,mPark,mCarType);
                }

            }
        });

        //上报
        mBinding.btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> file = new ArrayList<>();
                if (mBinding.picker.getPics()!=null){
                    if (mBinding.picker.getPics().size()<3){
                        showMes("图片不能少于3张");
                        return;
                    }
                }
                for (ImageItem item : mBinding.picker.getPics()){
                    if (item.path != null) {
                        file.add(item.path);
                    }
                }
                mPark.setFiles(file);
                mPresenter.updataPark(mPark,true);
            }
        });

    }

    @Override
    public void returnSpinnerCarType(List<CarType> list) {
        mBinding.spType.init(list);
        String number = getIntent().getStringExtra("number");
        String color = getIntent().getStringExtra("color");
        final String path = getIntent().getStringExtra("path");
        if(!TextUtils.isEmpty(number)&&number.length()>=7)
        {
            number= number.toUpperCase();
            final String sheng = number.substring(0, 1);
            final String letter = number.substring(1, 2);
            final String other = number.substring(2, number.length());
            String type=null;
            if("蓝".equals(color))
            {
                type="小型汽车";
            }else if("黄".equals(color))
            {
                type="大型汽车";

            }else if("绿".equals(color))
            {
                type="小型新能源汽车";
            }
            if(!TextUtils.isEmpty(type))
            {
                mBinding.spType.setSelection(mPresenter.getTypePosition(type));
            }

            ParkCheckDialog parkCheckDialog = new ParkCheckDialog(mContext);
            parkCheckDialog.setNumber(number);
            parkCheckDialog.setType(type);
            parkCheckDialog.setOnPosClick(new ParkCheckDialog.OnPosClick() {
                @Override
                public void onClick() {
                    mBinding.tvSheng.setText(sheng);
                    mBinding.tvLetter.setText(letter);
                    mBinding.etNum.setText(other);
//                    int i = path.lastIndexOf("/");
//                    String name=path.substring(i+1,path.length());
//                    mBinding.picker.addPic(new ImageItem(path,name,0));
                }
            });
            parkCheckDialog.setOnNegClick(new ParkCheckDialog.OnNegClick() {
                @Override
                public void onClick() {
                    File file = new File(path);
                    if(file.exists())
                    {
                        file.delete();
                    }
                    ARouter.getInstance().build("/lpark/memory")
                            .withString("ResultActivity","/lpark/park")
                            .withString("PATH", Environment.getExternalStorageDirectory().getAbsolutePath()+ "/lpark/park")
                            .navigation();
                    finish();
                }
            });
            parkCheckDialog.show();
        }else
        {
            showMes("未识别到车牌信息!");
        }
    }

    @Override
    public void returnLocation(AMapLocation reverseGeoCodeResult) {
        mBinding.etAddress.setText(reverseGeoCodeResult.getAddress());
        mPark.setLongitude(reverseGeoCodeResult.getLongitude()+"");
        mPark.setLatitude(reverseGeoCodeResult.getLatitude()+"");
        if (mPresenter.getRoadPosition(reverseGeoCodeResult.getStreet()) != -1) {
            mBinding.tvRoad.setText(reverseGeoCodeResult.getStreet());
        }
    }



    @Override
    public void returnSaveSuccess(String id, String objType,String noticeNo,boolean isUp) {
        mBinding.btnUp.setVisibility(View.VISIBLE);
        mBinding.btnPrint.setVisibility(View.VISIBLE);
        mBinding.btnSave.setVisibility(View.VISIBLE);
        mPark.setState("0");
        mPark.setId(id);
        mPark.setObjType(objType);
        mPark.setNoticeNo(noticeNo);
        mPark.setUserName(mPresenter.mUser.getUserName());
        mPark.setAddress(mPresenter.mUser.getAddress());
        mPark.setPhone(mPresenter.mUser.getPhone());
        if (isUp){
            mPresenter.ReportLLpark(mPark);
        }
    }

    @Override
    public void returnReportSuccess() {
        finish();
    }

    @Override
    public void returnNotice(String num) {
        CommonDialog commonDialog = new CommonDialog(mContext,"该车辆违停"+num+"次","提示","取消","确定");
        commonDialog.setOnPosClick(new CommonDialog.OnPosClick() {
            @Override
            public void onClick() {
                mPresenter.getCarType();
            }
        });
        commonDialog.setOnNegClick(new CommonDialog.OnNegClick() {
            @Override
            public void onClick() {
                mPresenter.getCarType();
            }
        });
        commonDialog.show();
    }

    @Override
    public void onBackPressedSupport() {
        mWaringDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
